package com.fyzk.activity.lottery.jc.beidan;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.lottery.R;
import com.fyzk.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 晒单方式
 */
public class GuoGuan_Pop extends PopupWindow implements View.OnTouchListener{

	private Context context;
	private View view;
	private GridView gridView;
	private GuoGuanAdapter adapter;

	private List<GuoGuanBean> guoGuanTypes = new ArrayList<>();

	private TextView more_tv;
	private LinearLayout more_ll;

	private int count = 2;
	private OnGuoGuanItemClickListener listener;

	public GuoGuan_Pop(Context context, View parent, List<GuoGuanBean> guoGuanTypes, OnGuoGuanItemClickListener listener){
		super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		this.context = context;
		this.guoGuanTypes = guoGuanTypes;
		this.listener = listener;
		View view_pop = LayoutInflater.from(context).inflate(R.layout.popup_guoguan, null);
		this.view = view_pop;
		this.view.setOnTouchListener(this);
		initView();
		this.setContentView(view);
		this.setFocusable(true);
		this.setOutsideTouchable(false);

		final int anchorLoc[] = new int[2];
		// 获取锚点parent在屏幕上的左上角坐标位置
		parent.getLocationOnScreen(anchorLoc);
		this.setHeight(anchorLoc[1]- ScreenUtils.getInstance().getStatusBarHeight()-3);
		// 刷新状态
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
//		this.setAnimationStyle(R.style.keyboard_anim);
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		this.dismiss();
//		popupDismiss(true);
		return true;
	}

	private void initView(){

		gridView = view.findViewById(R.id.guoguan_pop_gv);
		adapter = new GuoGuanAdapter(context, guoGuanTypes, listener);
		gridView.setAdapter(adapter);

		more_ll = view.findViewById(R.id.guoguan_pop_more_ll);
		more_tv = view.findViewById(R.id.guoguan_pop_more);
		more_tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (more_ll.getVisibility() == View.GONE) {
					more_ll.setVisibility(View.VISIBLE);
				} else {
					more_ll.setVisibility(View.GONE);
				}
			}
		});

	}

	/**
	 * 显示popupWindow
	 *
	 * @param parent
	 */
	public void showPopupWindow(View parent, List<GuoGuanBean> guoGuanTypes) {
		if (!this.isShowing()) {
			this.guoGuanTypes = guoGuanTypes;
			final int anchorLoc[] = new int[2];
			// 获取锚点View在屏幕上的左上角坐标位置
			parent.getLocationOnScreen(anchorLoc);
			super.showAtLocation(parent, Gravity.TOP,0,0);
			adapter.setGuoGuanTypes(guoGuanTypes);
		} else {
			this.dismiss();
		}

	}

	private void popupDismiss(boolean isCancel){
		if (!isCancel) {

		} else {

		}
		this.dismiss();
	}

	public void setGuoGuanTypes(List<GuoGuanBean> guoGuanTypes) {
		this.guoGuanTypes = guoGuanTypes;
		adapter.setGuoGuanTypes(guoGuanTypes);
	}

	//===== 接口	==========
	public interface OnGuoGuanItemClickListener {
		void onGuoGuanItemClick(List<GuoGuanBean> types);
	}

}
